package com.intellagric.controller.module.agriecom_quotation;


import com.intellagric.agriecom.module.agriecom_produce.ProduceService;
import com.intellagric.agriecom.module.agriecom_quotation.QuotationService;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.pojo.AgriecomProduce;
import com.intellagric.pojo.AgriecomQuotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class QuotationController {
    @Autowired
    private QuotationService quotationService;

    @Autowired
    private ProduceService produceService;

    @RequestMapping("/quotation/add")
    @ResponseBody
    public ResponseMessage addQuotation(AgriecomQuotation agriecomQuotation){

        //这里的agriecomQuotation需要在前端先给他赋值produce_id
        agriecomQuotation.setId(UUID.randomUUID().toString().replaceAll("-",""));
        agriecomQuotation.setRecordTime(new Date());
        //把价格循环添加七次添加：
        String trend=agriecomQuotation.getTrend();
        StringBuilder str=new StringBuilder();
        if(trend!=null&&!trend.equals("")) {
            for (int i = 1; i < 7; i++) {
                str.append(trend);
                str.append(",");
            }
            str.append(trend);
            agriecomQuotation.setTrend(str.toString());
        }
        return quotationService.addQuotation(agriecomQuotation);
    }

    @RequestMapping("/quotation/delete")
    @ResponseBody
    public ResponseMessage deleteQuotation(String id){
        return quotationService.deleteQuotation(id);
    }

    @RequestMapping("/quotation/batchDelete")
    @ResponseBody
    public ResponseMessage batchDeleteQuotation(@RequestParam(value="ids[]") String[] ids) {

        return quotationService.batchDeleteQuotation(ids);
    }

    @RequestMapping("/quotation/edit")
    @ResponseBody
    public ResponseMessage editQuotation(AgriecomQuotation agriecomQuotation){
        return quotationService.editQuotation(agriecomQuotation);
    }

    @RequestMapping("/quotation/{id}")
    @ResponseBody
    public AgriecomQuotation getQuotationById(@PathVariable String id){
        return quotationService.getQuotationById(id);
    }

    @RequestMapping("/quotation/list")
    @ResponseBody
    public LayuiDataGridResult getQuotationList(){
        return quotationService.getQuotationList();
    }

    @RequestMapping("/quotation/page")
    @ResponseBody
    public LayuiDataGridResult getQuotationList(int page,int limit){
        return quotationService.getQuotationList(page,limit);
    }

    @RequestMapping("/quotation/trend")
    @ResponseBody
    public String[] priceTrend(@RequestParam(name = "id")String id){
        AgriecomQuotation quotation=quotationService.getQuotationById(id);
        String [] strs=quotation.getTrend().split(",");

        return  strs;
    }


    /**
     * 根据分类查询行情
     * @param limit
     * @param page
     * @param categoryId
     * @return
     */
    @RequestMapping("/quotation/list/{categoryId}")
    @ResponseBody
    public LayuiDataGridResult getListByCategoryId(@RequestParam(name="limit",defaultValue = "15")int limit,
                                                   @RequestParam(name="page",defaultValue = "1")int page,
                                                   @PathVariable(value="categoryId")String categoryId){
        LayuiDataGridResult lay=produceService.getListByCategory(categoryId);
        List<AgriecomProduce> list=lay.getData();
        return quotationService.getQuotationByCategory(list,page,limit);

    }

    @RequestMapping("/quotation/deleteByProId")
    @ResponseBody
    public LayuiDataGridResult deleteQuotationByProId(@RequestParam("ids[]")String [] ids){
        return quotationService.deleteByProIds(ids);
    }

    @RequestMapping("/quotation/pricechange")
    @ResponseBody
    public String priceChange(String id,int price){

        try {
            return quotationService.priceChange(id,price).getMessage();
        } catch (ParseException e) {
            e.printStackTrace();
        }


        return "failure";
    }



    @RequestMapping("/quotation/quotationList")
    @ResponseBody
    public LayuiDataGridResult query(int page,int limit,String keyword){
        return   quotationService.getquotationByKeyword(page,limit,keyword);

    }

}
